<?php
/********************************************************************************
 * Screen Functions
 *
 * Screen functions are the controllers of BuddyPress. They will execute when their
 * specific URL is caught. They will first save or manipulate data using business
 * functions, then pass on the user to a template file.
 */
function dpa_add_admin_menu() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return false;

	bp_core_add_admin_menu_page( array(
		'menu_title' => __( 'Achievements', 'dpa' ),
		'page_title' => __( 'Achievements', 'dpa' ),
		'access_level' => 'manage_options', 'file' => 'achievements',
		'function' => 'dpa_screen_admin_edit'
	) );

	add_submenu_page( 'achievements', __( 'Achievements - Edit', 'dpa'), __( 'Achievements', 'dpa' ), 'manage_options', 'achievements', 'dpa_screen_admin_edit' );

	add_submenu_page( 'achievements', __( 'Achievements - Categories', 'dpa'), __( 'Categories', 'dpa' ), 'manage_options', 'achievements_categories', 'dpa_screen_admin_categories' );

	add_submenu_page( 'achievements', __( 'Achievements - Settings', 'dpa'), __( 'Settings', 'dpa' ), 'manage_options', 'achievements_settings', 'dpa_screen_admin_settings' );
}
add_action( 'admin_menu', 'dpa_add_admin_menu' );

function dpa_screen_admin_edit_category( $category_id ) {
	global $bp, $wpdb;

	if ( !is_site_admin() )
		return;

	$category = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->achievements->table_categories} WHERE id=%d LIMIT 1", $category_id ) );
	if ( !$category ) {
		bp_core_add_message( __( 'There was an error accessings the database; please try again.', 'dpa' ), 'error' );
		bp_core_redirect( $bp->root_domain );
		return;
	}
?>
	<div class="wrap">

		<div id="icon-options-general" class="icon32"><br /></div>
		<h2><?php _e( 'Edit Category', 'dpa' ) ?></h2>

		<form action="<?php echo site_url() . "/wp-admin/admin.php?page=achievements_categories&amp;action=save&amp;category=$category_id" ?>" method="post">
			<?php wp_nonce_field( 'category_save' ) ?>

			<div id="poststuff">

				<div class="titlediv">
					<h3><label for="category_name"><?php _e( 'Name', 'dpa' ) ?></label></h3>
					<div class="titlewrap">
						<input type="text" name="category_name" id="category_name" value="<?php echo esc_attr( apply_filters( 'dpa_admin_category_name_edit_value', $category->category ) ) ?>" style="width:50%" />
					</div>
				</div>

			</div>

			<div class="controls">
				<input type="reset" value="Reset" class="button" type="reset" />
				<input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'dpa' ) ?>" />
			</div>
		</form>
	</div>
<?php
}

function dpa_screen_admin_edit_achievement( $achievement_id ) {
	global $bp, $wpdb;

	if ( !is_site_admin() )
		return;

	$achievement = $wpdb->get_row( $wpdb->prepare( "SELECT category_id, description, nice_name, points FROM {$bp->achievements->table_achievements} WHERE id=%d LIMIT 1", $achievement_id ) );
	if ( !$achievement ) {
		bp_core_add_message( __( 'There was an error accessings the database; please try again.', 'dpa' ), 'error' );
		bp_core_redirect( $bp->root_domain );
		return;
	}
?>
	<div class="wrap">

		<div id="icon-options-general" class="icon32"><br /></div>
		<h2><?php _e( 'Edit Achievement', 'dpa' ) ?></h2>

		<form action="<?php echo site_url() . "/wp-admin/admin.php?page=achievements&amp;action=save&amp;achievement=$achievement_id" ?>" method="post">
			<?php wp_nonce_field( 'achievement_save' ) ?>

			<div id="poststuff">

				<div class="titlediv">
					<h3><label for="achievement_name"><?php _e( 'Name', 'dpa' ) ?></label></h3>
					<div class="titlewrap">
						<input type="text" name="achievement_name" id="achievement_name" value="<?php echo esc_attr( apply_filters( 'dpa_admin_achievement_name_edit_value', $achievement->nice_name ) ) ?>" style="width:50%" />
					</div>
				</div>

				<div class="titlediv">
					<h3><label for="achievement_description"><?php _e( 'Description', 'dpa' ) ?></label></h3>
					<div class="titlewrap">
						<textarea name="achievement_description" id="achievement_description" rows="8" cols="60"><?php echo esc_attr( apply_filters( 'dpa_admin_achievement_description_edit_value', $achievement->description ) ) ?></textarea>
					</div>
				</div>

				<div class="titlediv">
					<h3><label for="achievement_category"><?php _e( 'Category', 'dpa' ) ?></label></h3>
					<div class="titlewrap">
						<select name="achievement_category" id="achievement_category" style="width: 30%">
							<?php $categories = $wpdb->get_results( $wpdb->prepare( "SELECT category, id FROM {$bp->achievements->table_categories}" ) ); ?>
							<?php foreach ( $categories as $category ) { ( $category->id == $achievement->category_id) ? $selected = ' selected' : $selected = ''; ?>
							<option value="<?php echo esc_attr( apply_filters( 'dpa_admin_achievement_categoryid_edit_value', $category->id ) ) ?>" <?php echo $selected ?>><?php echo esc_attr( apply_filters( 'dpa_admin_achievement_category_edit_value', $category->category ) ) ?></option>
							<?php } ?>
						</select>
					</div>
				</div>

				<div class="titlediv">
					<h3><label for="achievement_points"><?php _e( 'Points', 'dpa' ) ?></label></h3>
					<div class="titlewrap">
						<input type="text" name="achievement_points" id="achievement_points" type="number" value="<?php echo esc_attr( apply_filters( 'dpa_admin_achievement_points_edit_value', $achievement->points ) ) ?>" style="width:50%" />
					</div>
				</div>

			</div>

			<div class="controls">
				<input type="reset" value="Reset" class="button" type="reset" />
				<input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'dpa' ) ?>" />
			</div>
		</form>
	</div>
<?php
}

function dpa_screen_admin_settings() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return;

	bp_core_setup_message();  // To trigger error/success feedback; doesn't usually work on admin pages.

	if ( isset( $_REQUEST['action'] ) ) {

		if ( 'retroactive' == $_REQUEST['action'] ) {
			check_admin_referer( "achievements_retroactive" );
			dpa_recalculate_userpoints();
		} else {
			bp_core_redirect( $bp->root_domain );
			return;
		}
	}
?>
	<div class="wrap">

		<div id="icon-options-general" class="icon32"><br /></div>
		<h2><?php _e( 'Achievements Settings', 'dpa' ) ?></h2>
		<?php do_action( 'template_notices' ); // (error/success feedback) ?>

		<div id="poststuff">

			<div class="titlediv">
				<p><?php _e( 'Use this option to look at everything your users have already done on your website so we can find out if any Achievement would have already been unlocked. It is a good idea to do this after installation. This will send out an email to the user for every Achievement unlocked.', 'dpa' )?></p>
				<div class="titlewrap">
					<p><a class="button" href="<?php echo wp_nonce_url( site_url() . '/wp-admin/admin.php?page=achievements_settings&amp;action=retroactive', 'achievements_retroactive' ) ?>"><?php _e( 'Retroactively Award Achievements', 'dpa' ) ?></a></p>
				</div>
			</div>

		</div>

	</div>
<?php
}

function dpa_screen_admin_categories() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return;

	bp_core_setup_message();  // To trigger error/success feedback; doesn't usually work on admin pages.

	if ( isset( $_REQUEST['action'] ) ) {

		$category_id = 0;
		if ( isset( $_REQUEST['category'] ) )
			$category_id = (int) $_REQUEST['category'];

		switch ( $_REQUEST['action'] ) {
			case 'add':
				$action = 'add';
				break;
			case 'edit':
				dpa_screen_admin_edit_category( $category_id );
				return;
			case 'remove':
				$action = 'remove';
				break;
			case 'save':
				$action = 'save';
				break;
			default:
				bp_core_redirect( $bp->root_domain );
				return;
		}

		check_admin_referer( "category_$action" );
		wp_cache_delete( 'dpa_active_categories', 'dpa' );
		dpa_update_category( $category_id, $action );
	}
	
	$categories = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->achievements->table_categories}" ) );
?>
	<div class="wrap">

		<div id="icon-options-general" class="icon32"><br /></div>
		<h2><?php _e( 'Manage Categories', 'dpa' ) ?></h2>
		<?php do_action( 'template_notices' ); // (error/success feedback) ?>

		<h3><?php _e( 'Current Categories', 'dpa' ) ?></h3>
		<table class="widefat achievements-categories" cellspacing="0">
			<thead>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Category', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</thead>

			<tbody class="plugins">
				<?php foreach ( $categories as $category ) : ?>

				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<td class="name"><?php echo apply_filters( 'dpa_get_achievement_category', $category->category ) ?></td>
					<td><a class="button" href="<?php echo wp_nonce_url( site_url() . '/wp-admin/admin.php?page=achievements_categories&amp;action=remove&amp;category=' . intval( $category->id ), 'category_remove' ) ?>"><?php _e( 'Remove', 'dpa' ) ?></a> <a class="button" href="<?php echo site_url() . '/wp-admin/admin.php?page=achievements_categories&amp;action=edit&amp;category=' . intval( $category->id ); ?>"><?php _e( 'Edit', 'dpa' ) ?></a></td>
				</tr>

				<?php endforeach; ?>
			</tbody>

			<tfoot>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Category', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</tfoot>
		</table>

		<h3><?php _e( 'Add Category', 'dpa' ) ?></h3>
		<form action="<?php echo site_url() . "/wp-admin/admin.php?page=achievements_categories&amp;action=add" ?>" method="post">
			<?php wp_nonce_field( 'category_add' ) ?>

			<div id="poststuff">

				<div class="titlediv">
					<h3><label for="category_name"><?php _e( 'Name', 'dpa' ) ?></label></h3>
					<div class="titlewrap">
						<input type="text" name="category_name" id="category_name" style="width:50%" />
					</div>
				</div>

			</div>

			<div class="controls">
				<input type="reset" value="Reset" class="button" type="reset" />
					<input type="submit" class="button" value="<?php _e( 'Add Category', 'dpa' ) ?>" />
			</div>
		</form>
	</div>
<?php
}

function dpa_screen_admin_edit() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return;

	bp_core_setup_message();  // To trigger error/success feedback; doesn't usually work on admin pages.

	if ( isset( $_REQUEST['achievement'] ) && isset( $_REQUEST['action'] ) ) {
		$achievement_id = (int) $_REQUEST['achievement'];

		switch ( $_REQUEST['action'] ) {
			case 'enable':
				$action = 'enable';
				break;
			case 'disable':
				$action = 'disable';
				break;
			case 'edit':
				dpa_screen_admin_edit_achievement( $achievement_id );
				return;
			case 'save';
				$action = 'save';
				break;
			default:
				bp_core_redirect( $bp->root_domain );
				return;
		}

		check_admin_referer( "achievement_$action" );
		wp_cache_delete( 'dpa_active_categories', 'dpa' );
		dpa_update_achievement( $achievement_id, $action );
	}
?>
	<div class="wrap">
		<div id="icon-options-general" class="icon32"><br /></div>
		<h2><?php _e( 'Manage Achievements', 'dpa' ) ?></h2>
		<?php do_action( 'template_notices' ); // (error/success feedback) ?>

		<h3><?php _e( 'Currently Active Achievements', 'dpa' ) ?></h3>
		<p><?php _e( 'Achievements that appear in the list below are active for the entire site.', 'dpa' ) ?></p>

		<table class="widefat achievements-enabled" cellspacing="0">
			<thead>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</thead>

			<tbody class="plugins">
				<?php if ( dpa_has_achievements( array( 'type' => 'enabled' ) ) ) : while ( dpa_user_achievements() ) : dpa_the_achievement() ?>

				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<td class="name"><?php dpa_achievement_name() ?></td>
					<td class="desc"><p><?php dpa_achievement_description() ?></p></td>
					<td><a class="button" href="<?php echo wp_nonce_url( site_url() . '/wp-admin/admin.php?page=achievements&amp;action=disable&amp;achievement=' . dpa_get_achievement_id(), 'achievement_disable' ) ?>"><?php _e( 'Disable', 'dpa' ) ?></a> <a class="button" href="<?php echo site_url() . '/wp-admin/admin.php?page=achievements&amp;action=edit&amp;achievement=' . dpa_get_achievement_id() ?>"><?php _e( 'Edit', 'dpa' ) ?></a></td>
				</tr>

				<?php endwhile; endif; ?>
			</tbody>

			<tfoot>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</tfoot>
		</table>

		<h3><?php _e( 'Other Achievements', 'dpa' ) ?></h3>
	
		<table class="widefat achievements-disabled" cellspacing="0">
			<thead>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</thead>

			<tbody class="plugins">
				<?php if ( dpa_has_achievements( array( 'type' => 'disabled' ) ) ) : while ( dpa_user_achievements() ) : dpa_the_achievement() ?>

				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<td class="name"><?php dpa_achievement_name() ?></td>
					<td class="desc"><p><?php dpa_achievement_description() ?></p></td>
					<td><a class="button" href="<?php echo wp_nonce_url( site_url() . '/wp-admin/admin.php?page=achievements&amp;action=enable&amp;achievement=' . dpa_get_achievement_id(), 'achievement_enable' ) ?>"><?php _e( 'Enable', 'dpa' ) ?></a> <a class="button" href="<?php echo site_url() . '/wp-admin/admin.php?page=achievements&amp;action=edit&amp;achievement=' . dpa_get_achievement_id() ?>"><?php _e( 'Edit', 'dpa' ) ?></a></td>
				</tr>

				<?php endwhile; endif; ?>
			</tbody>

			<tfoot>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</tfoot>
		</table>

	</div>
<?php
}
?>